package com.duowei.diandan.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.duowei.diandan.R;
import com.duowei.diandan.bean.food.OrderItem;
import com.duowei.diandan.callback.OrderItemDiffCallback;
import com.duowei.diandan.databinding.CartRecyclerItemBinding;
import com.duowei.diandan.databinding.OrderChildListItemBinding;
import com.duowei.diandan.db.entry.Wmlsb;
import com.duowei.diandan.fragment.OrderFragment;
import com.duowei.diandan.presenter.OrderItemChildPresenter;
import com.duowei.diandan.presenter.OrderItemPresenter;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexboxLayoutManager;

import java.util.ArrayList;
import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {

    private OrderFragment mFragment;
    private List<OrderItem> mList;

    public OrderAdapter(OrderFragment fragment, List<OrderItem> list) {
        mFragment = fragment;
        mList = list;
    }

    public void clear() {
        if (mList != null) {
            mList.clear();
        }

        notifyDataSetChanged();
    }

    public void updateData(List<OrderItem> newList) {
        OrderItemDiffCallback callback = new OrderItemDiffCallback(mList, newList);
        mList = newList;
        DiffUtil.DiffResult result = DiffUtil.calculateDiff(callback);
        result.dispatchUpdatesTo(this);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        CartRecyclerItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(
                context), R.layout.recycler_item_order, parent, false);
        return new ViewHolder(mFragment, binding);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.bind(mList.get(position));
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private Context mContext;
        private OrderFragment mFragment;
        private CartRecyclerItemBinding mBinding;

        public ViewHolder(OrderFragment fragment, CartRecyclerItemBinding binding) {
            super(binding.getRoot());
            mFragment = fragment;
            mBinding = binding;
            mContext = binding.getRoot().getContext();
        }

        public void bind(OrderItem item) {
            // --
            String pz = item.getMain().getPz();
            setTasteLayout(mBinding.tasteRecyclerView, pz);

            // ---
            mBinding.childLayout.removeAllViews();
            if (item.getChildList() != null && item.getChildList().size() > 0) {
                for (Wmlsb w : item.getChildList()) {
                    OrderChildListItemBinding binding = DataBindingUtil.inflate(
                            LayoutInflater.from(mContext), R.layout.list_item_order_child,
                            mBinding.childLayout, false);

                    binding.setWmlsb(w);
                    binding.setPresenter(new OrderItemChildPresenter(mFragment));
                    setTasteLayout(binding.tasteRecyclerView, w.getPz());

                    mBinding.childLayout.addView(binding.getRoot());
                }
            }

            // --
            mBinding.setItem(item);
            mBinding.setPresenter(new OrderItemPresenter(mFragment));
            mBinding.executePendingBindings();
        }

        static void setTasteLayout(RecyclerView tasteRecyclerView, String pz) {
            if (!TextUtils.isEmpty(pz)) {
                // pz = (番茄鸡肉)(加冰)(不加冰)(餐前)<备注>
                String[] array = pz.split("[()<>]");
                List<String> list = new ArrayList<>();
                for (String s : array) {
                    if (s.length() != 0)
                        list.add(s);
                }

                FlexboxLayoutManager layoutManager = new FlexboxLayoutManager(tasteRecyclerView.getContext());
                layoutManager.setFlexDirection(FlexDirection.ROW);
                tasteRecyclerView.setLayoutManager(layoutManager);
                tasteRecyclerView.setHasFixedSize(true);

                TasteShowAdapter tasteAdapter = new TasteShowAdapter(list);
                tasteRecyclerView.setVisibility(View.VISIBLE);
                tasteRecyclerView.setAdapter(tasteAdapter);
            } else {
                tasteRecyclerView.setVisibility(View.GONE);
                tasteRecyclerView.setAdapter(null);
            }
        }
    }
}
